<!--
 * @Author: your name
 * @Date: 2022-02-15 22:03:30
 * @LastEditTime: 2022-02-15 22:36:54
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \code\13.高阶函数&函数的柯里化\01.使用高阶函数和柯里化解决表单元素回调过多的问题.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01.使用高阶函数和柯里化解决表单元素回调过多的问题</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
</head>

<body>
    <div id="app"></div>
    <script type="text/babel">
        class LoginForm extends React.Component {
            state = {
                username: '',
                password: ''
            }
            render(){
                return (
                    <form onSubmit={this.submitForm}>
                        用户名：<input type="text" onChange={this.handleTyping('username')}/>
                        密码：<input type="password" onChange={this.handleTyping('password')}/>
                        <button>提交</button>
                    </form>
                )
            }

            // 柯里化高阶函数，事件回调函数在返回中，将 stateName 传入事件处理函数
            handleTyping = (stateName) => {
                return (event) => {
                    this.setState({
                        [stateName]: event.target.value
                    })
                }
            }
        
            submitForm = (event) => {
                event.preventDefault();
                const {username, password} = this.state
                alert(`username: ${username}  password: ${password}`)
            }
        }
        ReactDOM.render(<LoginForm/>, document.querySelector('#app'))
    </script>
</body>

</html>